package com.cswnode.demo.controller;

import com.cswnode.demo.entity.Page;
import com.cswnode.demo.entity.Profession;
import com.cswnode.demo.service.ProfessionService;
import com.cswnode.demo.util.ResponseVo;
import com.cswnode.demo.vo.ProfessionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author csw
 * @create 2021/3/8 11:12
 */
@Api(tags = "专业管理")
@RestController
@RequestMapping("/profession")
public class ProfessionController {

    @Autowired
    private ProfessionService professionService;

    @ApiOperation(value = "添加专业")
    @PostMapping("/add")
    public ResponseVo add(@RequestBody Profession profession){
        if (profession == null){
            return ResponseVo.error("专业不能为空！");
        }
        Profession insert = professionService.insert(profession);
        if (insert == null){
            return ResponseVo.error("添加失败！");
        }
        return ResponseVo.success("添加成功！");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/query")
    public ResponseVo query(@RequestParam(defaultValue = "1") Integer current,
                            @RequestParam(defaultValue = "10")Integer limit, String proName){
        Map<String,Object> result = new HashMap<>();
        Page page = new Page();
        page.setCurrent(current);
        page.setLimit(limit);
        page.setRows(professionService.queryNum(proName));
        List<ProfessionVo> professionVos = professionService.queryVoByLimit(page.getOffset(), page.getLimit(), proName);
        result.put("page",page);
        result.put("professionVos",professionVos);
        return ResponseVo.success(result);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/queryAll")
    public ResponseVo query(String proName){
        List<Profession> profession = professionService.query(proName);
        return ResponseVo.success(profession);
    }

    @ApiOperation(value = "修改专业")
    @PostMapping("/update")
    public ResponseVo update(@RequestBody Profession profession){
        if (ObjectUtils.isEmpty(profession)){
            return  ResponseVo.error("专业信息不得为空！");
        }
        String result = professionService.update(profession);
        if (StringUtils.isEmpty(result)){
            return ResponseVo.success("修改成功！");
        }
        return ResponseVo.error(result);
    }

    @ApiOperation(value = "删除专业")
    @GetMapping("/delete/{proId}")
    public ResponseVo delete (@PathVariable("proId")String proId){
        if (StringUtils.isEmpty(proId)){
            return ResponseVo.error("删除失败！");
        }
        boolean result = professionService.deleteById(proId);
        if (!result){
            return ResponseVo.error("删除失败");
        }
        return ResponseVo.success("删除成功");
    }
}
